package com.sgz.androidlib;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.android.sgzcommon.activity.utils.MUploadResultSet;
import com.android.sgzcommon.fragment.BaseGetPhotoListFragment;
import com.android.sgzcommon.http.okhttp.upload.OKUploadTask;
import com.android.sgzcommon.http.okhttp.upload.OnUploadFileListener;
import com.android.sgzcommon.http.okhttp.upload.UploadResultSet;
import com.android.sgzcommon.take_photo.entity.PhotoUpload;
import com.android.sgzcommon.take_photo.listener.OnPhotoClickListener;
import com.android.sgzcommon.take_photo.utils.Creator;
import com.android.sgzcommon.view.SuButton;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author sgz
 * @date 2021-02-23
 */
public class NavSecondFragment extends BaseGetPhotoListFragment<PhotoUpload> {
    @BindView(R.id.rv_list)
    RecyclerView mRvList;
    @BindView(R.id.btn_upload)
    SuButton mBtnUpload;
    private List<PhotoUpload> mPhotoUploadList;

    @Override
    protected int getShowImageGridViewId() {
        return 0;
    }

    @Override
    protected int getTakePhotoGridViewId() {
        return R.id.rv_list;
    }

    @Override
    protected Creator<PhotoUpload> getCreator() {
        return null;
    }

    @Override
    protected void onPhotos(List<PhotoUpload> uploads, PhotoUpload upload) {
        mPhotoUploadList.clear();
        mPhotoUploadList.addAll(uploads);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_get_photo_list;
    }

    @Override
    protected void init(Bundle savedInstanceState, View parent) {
        super.init(savedInstanceState, parent);
        mPhotoUploadList = new ArrayList<>();
        setOnPhotoClickListener(new OnPhotoClickListener() {
            @Override
            public void onClick(View view) {
                choosePhoto();
            }

            @Override
            public void onDelete(int position, PhotoUpload upload) {

            }
        });
    }

    @OnClick(R.id.btn_upload)
    public void onViewClicked() {
        for (int i = 0; i < mPhotoUploadList.size(); i++) {
            final PhotoUpload image = mPhotoUploadList.get(i);
            if (PhotoUpload.STATE.STATE_UPLOAD_READY == image.getState() || PhotoUpload.STATE.STATE_UPLOAD_FAIL == image.getState()) {
                Log.d("NavSecondFragment", "checkAndUploadAllPhotos: upload......");
                final List<PhotoUpload> photoUploadList = new ArrayList<>();
                photoUploadList.add(image);
                Map<String, String> data = new HashMap<>();
                OKUploadTask.getInstance().upLoadFiles("http://192.168.0.106:80/api/uploadController.do?ajaxSaveFile", photoUploadList, data, null, new MUploadResultSet(), new OnUploadFileListener<PhotoUpload>() {

                    @Override
                    public void onUploadStart(String url) {

                    }

                    @Override
                    public void onUploadSuccess(String url, List<PhotoUpload> inspectionPhotoUploads, UploadResultSet result) {
                        Log.d("NavSecondFragment", "onUploadSuccess: " + result.getResponse());

                    }

                    @Override
                    public void onUploadFail(String url, List<PhotoUpload> inspectionPhotoUploads, String e) {

                    }

                    @Override
                    public void onEntityStart(PhotoUpload upload) {

                    }

                    @Override
                    public void onEntityValue(PhotoUpload upload, int value) {
                        Log.d("NavSecondFragment", "onEntityValue: value == " + value);
                    }

                    @Override
                    public void onEntitySuccess(PhotoUpload upload) {

                    }

                    @Override
                    public void onEntityFail(PhotoUpload upload, String e) {

                    }
                });
            }
        }
    }
}
